package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.SiteService;

/**
 * 站点管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("v1.0/sites")
public class SiteController {

	@Resource
	private SiteService siteService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listSite(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			respJson = siteService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【详情】
	 * 
	 * @param siteId
	 *            站点编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{site}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getSite(@PathVariable("site") String siteId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = siteService.get(siteId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【注册】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@Auth(value = { UserRoleConstants.ADMIN })
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson registerSite(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = siteService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【注销】
	 * 
	 * @param siteId
	 *            站点编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{site}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson deregisterSite(@PathVariable("site") String siteId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = siteService.remove(siteId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
